---@class Battle.ActionStateSystem:Battle.EntityComponent
local ActionStateSystem = Battle.CreateClass("ActionStateSystem",Battle.EntityComponent)
Battle.ActionStateSystem = ActionStateSystem
function ActionStateSystem:OnCreate()
    ---@type Battle.StateBase
    self.curActionState = nil
    self:RegUpdate(true)
end
function ActionStateSystem:OnDestroy()
    self.curActionState = nil
end
function ActionStateSystem:InState(stateId)
    if self.curActionState and self.curActionState.stateId == stateId then
        return true
    end
    return false
end
function ActionStateSystem:EnterState(stateId, data,forceExit)
    if self.actor == nil then return end
    local state = self.actor:GetActionState()
    if state and state.stateId == stateId then
        state:BaseEnter(self.actor,data)
        return
    end
    self:ExitState(forceExit)
    local newState = self:CreateState(stateId)
    if newState then
        self.curActionState = newState
        newState:BaseEnter(self.actor,data)
    end
end
function ActionStateSystem:IsStateFinished()
    if not self.curActionState then return false end
    return self.curActionState:IsFinished()
end

function ActionStateSystem:ExitState(forceExit)
    if self.actor == nil then return end
    local state = self.actor:GetActionState()
    if state ~= nil then
        state:BaseExit(forceExit)
        Battle.PoolMgr.ReturnStateToPool(state)
    end
end
---@return Battle.StateBase
function ActionStateSystem:CreateState(stateId)
    return Battle.PoolMgr.GetStateFromPool(stateId)
end

function ActionStateSystem:Update(dt)
    if not self.curActionState then return end
    if self.curActionState then
        self.curActionState:BaseUpdate(dt)
    end
    local nextId = self.curActionState:GetNextStateId()
    if self.curActionState:IsFinished() and nextId > 0 then
        self:EnterState(nextId)
    end
end

